package cn.edu.scau.cmi.chenenting.composite.consistent;

import java.util.ArrayList;

public class Composite implements Component {
	
	private String name;
	private Composite father;
	private ArrayList<Component> organization;
	public static String result = "";
	
	public Composite(String name) {
		this.name = name;
		organization = new ArrayList<Component>();
	}
	
	public void setFather(Composite father) {
		this.father = father;
	}
	
    public void add(Component child) {
    	child.setFather(this);
    	organization.add(child);
    }
    
    public void remove(Component child) {
    	Component c = null;
		for(int i = 0; i < organization.size(); i++) {
			if(organization.get(i).getName().equals(child.getName())) {
				c = organization.get(i);
			}
		}
		if(c != null) {
			organization.remove(c);
		}
    }
    
    public String getName() {
    	return this.name;
	}
    
	public Composite getFather() {
		return this.father;
	}
    
    public ArrayList<Component> getChild() {
        return organization;
    }
	
	public void display(String preStr) {
        result += preStr + "+" + this.name + "\n";
        if (this.organization != null) {
            preStr += "  ";
            for (Component c : organization) {
                c.display(preStr);
            }
        }
	}
	
}
